<?php

namespace app\modules\Front\controllers;

use yii;
use app\modules\Cruise\models\Cruise;
use app\modules\Cruise\models\Room;
use app\modules\Cruise\models\Image;
use app\modules\Cruise\models\Service;
use app\modules\Cruise\models\Restaurant;
use app\modules\Cruise\models\Recreation;
use app\modules\Product\models\Product;
use app\modules\Product\models\ProductRoom;
use app\modules\Order\models\OrderRoom;
use app\modules\Order\models\Order;
use app\modules\Order\models\OrderContact;
use app\modules\Member\models\Member;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;
use yii\data\Sort;
use yii\web\Controller;

use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\ManipulatorInterface;
use Imagine\Image\Point;
use Imagine\Image\Palette\RGB;
/**
 *  首页
 */
class DefaultController extends Controller
{
   

     //邮轮路线列表
    public function actionIndex()
    {
        $data = yii::$app->request->get();
        $today = date("Y-m-d");
        $query = Product::find();
        $query->joinWith('cruise'); 
        $query->andWhere(['product.status'=>1])->andFilterWhere(['>=','product.go_time',$today]);
        
        !empty($data['lines']) ? $query->andWhere(['product.line'=>$data['lines']]) :''; 

        !empty($data['from_city']) ? $query->andWhere(['product.from_city'=>$data['from_city']]) :'';

        !empty($data['cruise_id']) ? $query->andWhere(['product.cruise_id'=>$data['cruise_id']]) :'';

        if(empty($data['cruise_id']) && !empty($data['supplier_id'])){
            $query->andWhere(['cruise.supplier_id'=>$data['supplier_id']]);
        }

        if(!empty($data['day'])){
            if($data['day'] == 1){
                $query->andFilterWhere(['between', 'product.day', 1, 5]);   
            }elseif ($data['day'] == 2) {
                $query->andFilterWhere(['between', 'product.day', 6, 10]);
            }elseif ($data['day'] == 3) {
                $query->andFilterWhere(['between', 'product.day', 11, 15]);
            }elseif ($data['day'] == 4) {
                $query->andFilterWhere(['>', 'product.day', 15]);
            }  
        }

        $month = [];
        for($i=0;$i<12;$i++){
            $firstday = date("Y-m-01"); 
            $month[$i+1] = date("Y年m月",strtotime("$firstday +$i month"));
        }
        if(!empty($data['month'])){
            $i_day = $data['month']-1;
            $beginDate = date('Y-m-01', strtotime("+$i_day month"));
            $endDate = date('Y-m-d', strtotime("$beginDate +1 month -1 day"));
            $query->andFilterWhere(['>=', 'product.go_time', $beginDate])->andFilterWhere(['<=', 'product.go_time', $endDate]);
        }    
        
        $sort = 0;
        if(!empty($data['sort'])){
           $sort = $data['sort'];
           if($sort == 3){
               $sort_order = ["min_price"=>SORT_ASC];
           }else if($sort == 4){
               $sort_order = ["min_price"=>SORT_DESC];
           }else if($sort == 5){
               $sort_order = ["day"=>SORT_ASC];
           }else if($sort == 6){
               $sort_order = ["day"=>SORT_DESC];
           }else if($sort == 7){
               $sort_order = ["go_time"=>SORT_ASC];
           }else if($sort == 8){
               $sort_order = ["go_time"=>SORT_DESC];
           }
        }else{
           $sort_order = ['go_time'=>SORT_ASC];
        }

        $all_lines = $query->all();

        $lines = [];

        if(!empty($data['price'])){
            $min_price = 0;
            $max_price = 0;
            if($data['price'] == 1){
                $min_price = 0;
                $max_price = 3000;
            }else if($data['price'] == 2){
                $min_price = 3001;
                $max_price = 5000;
            }else if($data['price'] == 3){
                $min_price = 5001;
                $max_price = 6000;
            }else if($data['price'] == 4){
                $min_price = 6001;
                $max_price = 7000;
            }else if($data['price'] == 5){
                $min_price = 7001;
                $max_price = 8000;
            }else if($data['price'] == 6){
                $min_price = 8001;
                $max_price = 10000;
            }else if($data['price'] == 7){
                $min_price = 10001;
                $max_price = 300000;
            }
            foreach($all_lines as $v){
                $v->min_price = $v->minPrice;
                if($v->minPrice >= $min_price && $v->minPrice <=$max_price){
                    $lines[] = $v;
                }  
            }
        }else{
           foreach($all_lines as $v){
               $v->min_price = $v->minPrice;
               $lines[] = $v;
           }
        }    

       
       
        $provider = new ArrayDataProvider([
              'allModels' => $lines,
              'sort' => [ 
                  'attributes' => ['go_time', 'day', 'min_price'],
                  'defaultOrder' => $sort_order,
              ],
              'pagination' => [
                  'pageSize' => 15,
                  'defaultPageSize'=>15,
              ],
        ]);
        $lines = $provider->getModels();
        
        return $this->render('index',['lines'=>$lines,'provider'=>$provider,'params'=>$data,'month'=>$month,'today'=>$today,'sort'=>$sort]);
    }


    //航线详情
    public function actionLines($id)
    {

        $today = date("Y-m-d");
        //航线
        $models = Product::find()->where(['product.id'=>$id])->one();   
        //邮轮
        $cruise = Cruise::find()->where(['id'=>$models->cruise_id])->one();
        //航线价格
        $linesPrice = ProductRoom::find()->where(['product_id'=>$id,'show'=>1])->andWhere(['not', ['price' => null]])->orderBy('sort desc')->all();
        
        $in_room = $sea_room = $bal_room = $sui_room = $high_room = $other_room = [];//1.内舱房  2海景 3阳台 4套房 6其它
           
        $room_type = [
            1=>'单人间', 2=>'双人间', 3=>'三人间',4=>'四人间',5=>'五人间',
            6=>'六人间',7=>'七人间',8=>'八人间',9=>'九人间',10=>'十人间',
        ];

        if(!empty($linesPrice)){
            foreach($linesPrice as $rooms){
                    $price_room = [
                        'name'  => sprintf("%s%s",$rooms->room->name,$room_type[$rooms->people]),
                        'code'  => $rooms->room->code,
                        'price' => $rooms->price,
                        'ceil'  => $rooms->room->ceil,
                        'inside_price'=>$rooms->inside_price,
                    ]; 
                   
                    if($rooms->room->type == 1){
                        $in_room[] = $price_room;
                    }elseif($rooms->room->type ==2){
                        $sea_room[] = $price_room;
                    }elseif($rooms->room->type == 3){
                        $bal_room[] = $price_room;   
                    }elseif($rooms->room->type == 4) {
                        $sui_room[] = $price_room;
                    }elseif($rooms->room->type == 5) {
                        $high_room[] = $price_room;
                    }elseif($rooms->room->type == 6) {
                        $other_room[] = $price_room;
                    }  
            } 
        }

        //邮轮图片
        $img = Image::find()->where(['product_id'=>$models->cruise_id,'type'=>1,'status'=>1])->orderby('cover desc')->all();

        //其他航线
        $otherlines = Product::find()
            ->where(['status'=>1,'cruise_id'=>$models->cruise_id])
            ->andFilterWhere(['not in','id',$id])
            ->andFilterWhere(['>=','go_time',$today])
            ->orderby('go_time asc')
            ->all();
          
        //获得库存
        $stock = $this->getStock($id);
       
    	  return  $this->render('lines',['models'=>$models,'img'=>$img,'cruise'=>$cruise,'otherlines'=>$otherlines,'in_room'=>$in_room,'sea_room'=>$sea_room,'sui_room'=>$sui_room,'bal_room'=>$bal_room,'high_room'=>$high_room,'other_room'=>$other_room,'linesPrice'=>$linesPrice,'stock'=>$stock]);
    }


    //获取航线库存
    private function getStock($id)
    {

        if(!is_numeric($id) ){
            throw new NotFoundHttpException('查看过程出现异常,请稍后重试!');
        }

        // 获取航线对应库存信息
        $model = Product::findOne($id);
        $total = 0;
        if($model->stock == 1){ //标准库存格式
        	$total = $model->stockAll;// 总数
        }else if($model->stock ==2){ //自定义库存格式 
        	$rooms = json_decode($model->rooms,true);
        	if(!empty($rooms)){
        		$total = array_sum(array_column($rooms,'num'));
        	}
        }
        $order = $model->allOrder;// 所有订单
        //$count = $model->allOrderNum;// 订单数
        $ids = yii\helpers\ArrayHelper::map($order, 'id', 'id', 'status');
            
        $occupy = [];
        $sell = [];
        foreach($ids as $key=>$val){
            if($key== 1){ // 占位
             $occupy += $val;
            }elseif($key == 2){ // 付定金
                $sell += $val;
            }elseif($key == 3){ // 已付款
                $sell += $val;
            }elseif($key == 4){ // 确认提交
                $sell += $val;
            }
        }
		//print_r($occupy);exit;
        $occupy_num = $model->stockSale($occupy);   //  占位舱房数量

        $sell_num = $model->stockSale($sell);     //  售出舱房数量

        $stock = $total - ($occupy_num + $sell_num); //剩余数
        // 剩余舱房数量
        $res = ['total' => $total,
                'sell_num' => $sell_num,
                'occupy_num' => $occupy_num,
                'stock' => $stock
                ];
        return $res;

    }

    //航线房间库存
    public function actionLineRoom($id)
    {

        if(!\Yii::$app->user->can('room_stock')){
            return $this->redirect("/member/member/login-url"); 
        }
        $stock = $this->getStock($id);    
    	$model = Product::findOne($id);
    	 	$room_type = [
    	        1=>'单人', 2=>'双人', 3=>'三人',4=>'四人',5=>'五人',
    	        6=>'六人',7=>'七人',8=>'八人',9=>'九人',10=>'十人',
         	];

         	$all_rooms = [];
    		if($model->stock == 1){ //标准库存格式
    			//获取该航线所有房间类型的价格
            	$all_rooms = ProductRoom::find()->where(['product_id'=>$id,'show'=>1])->andWhere(['not', ['price' => null]])->all();

    		}else if($model->stock == 2){ //自定义库存格式
    			if(!empty($model->rooms)){
    				$all_rooms = json_decode($model->rooms,true);
    			}
    			if(!empty($all_rooms)){
    				foreach($all_rooms as $key=>&$val){
    					$use_num = OrderRoom::find()->where(['room_id'=>$key,'product_id'=>$id,'status'=>1,'stock'=>1])->count();
    					$val['over_num'] = $val['num'] - $use_num;
    					$val['use_num'] = $use_num;
    					$val['room_id'] = $key;
    					$val['product_id'] = $id;
    				}
    			}
    		}

        return $this->render('line-room',['stock'=>$stock,'model'=>$model,'all_rooms'=>$all_rooms,]);
    }

    //航线占位详情
    public function actionSetRoomDetail()
    {  
        if(!\Yii::$app->user->can('room_create')){
            return false;
        }
        if(!\Yii::$app->user->can('room_stock')){
            return $this->redirect("/member/member/login-url"); 
        }
            
        $data = yii::$app->request->post();
        if(empty($data['product_id']) || empty($data['data'])){
            return false;
        }    
        $product = Product::findOne($data['product_id']);
        $data = json_decode($data['data']);
        

        return $this->render('set-room-detail',['data'=>$data,'product'=>$product]);
    }

    //新占位
    public function actionSetRoom()
    {   
       if(!\Yii::$app->user->can('room_create')){
            return false;
       }
       $data = yii::$app->request->post();
        
       if(!empty($data)){
            $product_id = $data['Order']['product_id'];
            if(!empty($product_id)){
                $params = [
                    'Order' => ['product_id'=>$product_id,'member_id'=>$data['Order']['member_id'],'source'=>$data['Order']['source'],'is_merge'=>$data['Order']['is_merge'],'status'=>1,'username'=>$data['Order']['username'],'phone'=>$data['Order']['phone'],'company'=>$data['Order']['company'],'remark'=>$data['Order']['remark']],
                    ];  
                if(is_array($data['room'])){
                     foreach($data['room'] as $vr){
                        if($vr['room_num'] > 0){
                            foreach($vr['num'] as $num){
                                $room_arr[] = ['id'=>$vr['room_id'],'num'=>$num];
                            }  
                            $params['room'] = $room_arr;    
                        } 
                     }
                }    
                
                $product = Product::findOne($product_id);

                $model = new Order();
                if($model->load($params) && $model->save()){
                    if($product->stock == 1){ //标准库存格式
                        Yii::$app->orderblock->create($params, $model);
                    }elseif($product->stock == 2){ //自定义库存格式
                        Yii::$app->orderblock->create2($params, $model);
                    }
                    $res = ['code'=>1,'data'=>'','msg'=>'占位成功'];
                    return $this->success(['line-room','id'=>$product_id]);
                }
            }
            $res = ['code'=>0,'data'=>'','msg'=>'占位失败'];
            print_r(json_encode($res));exit();
        }
       
    }

    //成功跳转页面
    public function success($url= [] ,$sec = 3){
        //$this->layout = "//main";
        $url= empty($url)? ['/front/default']: $url;
        $url= \yii\helpers\Url::toRoute($url);
        return $this->render('msg',['gotoUrl'=>$url,'sec'=>$sec]);
    } 


    //邮轮列表
    public function actionCruiseList()
    {
        $data = yii::$app->request->get();

        $query = Cruise::find();

        $query->joinWith('imageOne')->joinWith('supplier')->andWhere(['cruise.status'=>1]);

        !empty($data['supplier_id']) ? $query->andWhere(['cruise.supplier_id'=>$data['supplier_id']]) :'';
        if(!empty($data['sort'])){
             if($data['sort'] == 1){
               $query->orderby('cruise.weight desc'); 
            }elseif ($data['sort'] == -1) {
               $query->orderby('cruise.weight asc'); 
            }
        }
       
        $cruises = $query->asArray()->all();
        foreach ($cruises as $key =>&$value) {
             if(!empty($value['first'])){
                $value['year'] = $value['first'];
             }else{
                $value['year'] = $value['renovation']; 
             }
        }

         if(!empty($data['sort'])){
            $sort = '';
            if($data['sort'] == 2){
                $sort = SORT_DESC;  
            }elseif ($data['sort'] == -2){
                $sort = SORT_ASC;
            }
            if(!empty($sort)){
                 $provider  = new ArrayDataProvider([
                    'allModels' => $cruises,
                    'sort' => [
                        'attributes' => ['year'],
                        'defaultOrder'=>['year'=>$sort],
                    ],
                    'pagination' => [
                        'pageSize' => 999,
                    ],
                    
                ]); 
                $cruises = $provider->getModels();
            }
           
        }

        return $this->render('cruise-list',['cruises'=>$cruises,'params'=>$data]);

    }

    //邮轮详情
    public function actionCruise($id)
    {

        $today = date("Y-m-d");
        //图片
        $img = Image::find()->where(['product_id'=>$id,'type'=>1,'status'=>1])->orderby('cover desc')->all();
        //邮轮
        $cruise = Cruise::find()->where(['id'=>$id])->one();
        //仓舫
        $room = Room::find()->joinWith('image')->where(['room.cruise_id'=>$id,'room.status'=>1])->all();

        $in_room = $sea_room = $bal_room = $sui_room = $high_room = $other_room = [];//1.内舱房  2海景 3阳台 4普通套房 5 高级套房 6其它
        foreach($room as $v){
              if($v->type ==1){
                 $in_room[] = $v;
              }elseif($v->type == 2){
                 $sea_room[] = $v;
              }elseif($v->type == 3){
                 $bal_room[] = $v;
              }elseif($v->type == 4){
                 $sui_room[] = $v;   
              }elseif($v->type == 5){
                 $high_room[] = $v;   
              }elseif ($v->type == 6) {
                 $other_room[] = $v;
              }
        }      

        $data = yii::$app->request->get();

        $in_first = $sea_first = $bal_first = $sui_first = $high_first = $other_first = [];        
        if(isset($data['in_room_id'])){
             foreach($room as $val){
                 if($val['id'] == $data['in_room_id']){
                     $in_first = $val;
                     break;
                 }
             }
        }else{
            if(!empty($in_room)){
                 foreach ($in_room as $value) {
                       $in_first = $value;
                       break;
                 }
            }
         }
        if(isset($data['sea_room_id'])){
             foreach($room as $val){
                 if($val['id'] == $data['sea_room_id']){$sea_first = $val;break;}
             }
        }else{
            if(!empty($sea_room)){
                 foreach ($sea_room as $value) {$sea_first = $value;break;}
            }
        }
         
        if(isset($data['bal_room_id'])){
             foreach($bal_room as $val){
                 if($val['id'] == $data['bal_room_id']){$bal_first = $val;break;}
             }
         }else{
            if(!empty($bal_room)){
                 foreach ($bal_room as $value) {$bal_first = $value;break;}
            }
         }

        if(isset($data['sui_room_id'])){
             foreach($sui_room as $val){
                 if($val['id'] == $data['sui_room_id']){$sui_first = $val;break;}
             }
        }else{
            if(!empty($sui_room)){
                 foreach ($sui_room as $value) {$sui_first = $value;break;}
            }
         }

         if(isset($data['high_room_id'])){
             foreach($high_room as $val){
                 if($val['id'] == $data['high_room_id']){$high_first = $val;break;}
             }
        }else{
            if(!empty($high_room)){
                 foreach ($high_room as $value) {$high_first = $value;break;}
            }
         }

        if(isset($data['other_room_id'])){
             foreach($other_room as $val){
                 if($val['id'] == $data['other_room_id']){$other_first = $val;break;}
             }
         }else{
            if(!empty($other_room)){
                 foreach ($other_room as $value) {$other_first = $value;break;}
            }
         }
         
         //餐饮
        $restaurant = Restaurant::find()->joinWith('image')->where(['restaurant.cruise_id'=>$id,'restaurant.status'=>1])->all();

         //娱乐
        $recreation = Recreation::find()->joinWith('image')->where(['recreation.cruise_id'=>$id,'recreation.status'=>1])->all();
         
         //甲板
        $board = Image::find()->where(['product_id'=>$id,'type'=>6,'status'=>1])->orderby('cover desc')->all();

         //邮轮航线
        $lines = Product::find()->where(['cruise_id'=>$id,'status'=>1])->andFilterWhere(['>=','go_time',$today])->orderby('go_time asc')->all();
        
         //服务
        $service = Service::find()->joinWith('image')->where(['service.cruise_id'=>$id,'service.status'=>1])->all();
       
    	  return $this->render('cruise',['cruise'=>$cruise,'room'=>$room,'img'=>$img,'service'=>$service,
            'restaurant'=>$restaurant,'recreation'=>$recreation,'board'=>$board,'lines'=>$lines,'in_room'=>$in_room,'sea_room'=>$sea_room,'sui_room'=>$sui_room,'bal_room'=>$bal_room,'high_room'=>$high_room,'other_room'=>$other_room,'in_first'=>$in_first,'sea_first'=>$sea_first,'bal_first'=>$bal_first,'sui_first'=>$sui_first,'other_first'=>$other_first,'high_first'=>$high_first]);
    }



    //航线销售列表
    public function actionSaleList()
    {   

        if(!\Yii::$app->user->can('room_create')){
            return false;
        }

        if(\Yii::$app->user->isGuest){
            return $this->redirect("/member/member/login"); 
        }
        
        $today = date("Y-m-d");
        $query = Product::find()->where(['status'=>1])->andFilterWhere(['>=','go_time',$today])->orderby('go_time asc');

        $pages = new Pagination(['totalCount' =>$query->count(), 'pageSize' => '20']);

        $list = $query->offset($pages->offset)->limit($pages->limit)->all();

        $data = [];
        if(!empty($list)){
           foreach($list as $model){
                
                if($model->stock == 1){
                    if($model->getStockAll() == 0){
                      $data[$model->id] = [1=>0];
                    }else{
                      $data[$model->id] = $model->statusOrderRoom;
                    }
                }else if($model->stock == 2){
                    if($model->getStockAll2() == 0){
                       $data[$model->id] = [1=>0];
                    }else{
                       $data[$model->id] = $model->statusOrderRoom;
                    }
                    
                }
           }
        }
        $order_status = [1=>'占位房间',2=>'付定金房间',3=>'已付款房间',4=>'确认提交房间',100=>'剩余房间'];
       
        return  $this->render('sale-list',['list'=>$list,'pages'=>$pages,'data'=>$data,'order_status'=>$order_status]);
    }
   
    //航线销售详情
    public function actionSaleDetail($id)
    {   
        if(!\Yii::$app->user->can('room_create')){
            return false;
        }

        if(\Yii::$app->user->isGuest){
            return $this->redirect("/member/member/login"); 
        }

        $model = Product::findOne($id);

        return  $this->render('sale-detail',['model'=>$model]);
    }

    //销售订单列表
    public function actionOrderSaleList()
    {
        if(!\Yii::$app->user->can('room_create')){
            return false;
        }
        
        if(\Yii::$app->user->isGuest){
            return $this->redirect("/member/member/login"); 
        }

        if(Yii::$app->user->can('order_all')){
           $orders = Order::find();
        }else{
           $orders = Order::find()->where(['member_id'=>Yii::$app->user->identity->id]);
        }

        $pages = new Pagination(['totalCount' =>$orders->count(), 'pageSize' => '20']);

        $model = $orders->offset($pages->offset)->limit($pages->limit)->all();

        return $this->render('order-sale-list',['model' => $model,
             'pages' => $pages]);

    }

    //生成图片
    public function actionCreateImg($id){

        if(!\Yii::$app->user->can('room_stock')){
            return $this->redirect("/member/member/login-url"); 
        }
        //$stock = $this->getStock($id);    
        $model = Product::findOne($id);
        $room_type = [
            1=>'单人', 2=>'双人', 3=>'三人',4=>'四人',5=>'五人',
            6=>'六人',7=>'七人',8=>'八人',9=>'九人',10=>'十人',
        ];

        $all_rooms = [];
        if($model->stock == 1){ //标准库存格式
            //获取该航线所有房间类型的价格
            $all_rooms = ProductRoom::find()->where(['product_id'=>$id,'show'=>1])->andWhere(['not', ['price' => null]])->all();

        }else if($model->stock == 2){ //自定义库存格式
            if(!empty($model->rooms)){
                $all_rooms = json_decode($model->rooms,true);
            }
            if(!empty($all_rooms)){
                foreach($all_rooms as $key=>&$val){
                    $use_num = OrderRoom::find()->where(['room_id'=>$key,'product_id'=>$id,'status'=>1])->count();
                    $val['over_num'] = $val['num'] - $use_num;
                    $val['use_num'] = $use_num;
                    $val['room_id'] = $key;
                    $val['product_id'] = $id;
                }
            }
        }

        $num = 0;
        if($all_rooms){
            $num = count($all_rooms);    
        }
       
        $y = 280 + $num*60;
    
        $imagine = \yii\imagine\Image::getImagine();
        $size  = new Box(1400, $y);
        $palette = new RGB();
        $color = $palette->color('#fff');
        $image = $imagine->create($size,$color);

        //外边框
        $image->draw()
              ->polygon([new Point(20, 20),
                new Point(1380, 20),
                new Point(1380, $y-20),
                new Point(20, $y-20),], $palette->color('000'),false);
              
        //一条横线 高度
        $image->draw()->line(new Point(20,80),new Point(1380,80),$palette->color('000'));
        $image->draw()->line(new Point(20,140),new Point(1380,140),$palette->color('000'));
        $image->draw()->line(new Point(20,200),new Point(1380,200),$palette->color('000'));

        
        $path = 'font/msyh.ttf';
        $font = $imagine->font($path, 18, $palette->color('#000'));

        $image->draw()->text($model->name, $font, new Point(30, 40), 0);
        $image->draw()->text(sprintf("出发日期：%s %s出发",$model->go_time,\yii::$app->params['city'][$model->from_city]), $font, new Point(30, 100), 0);
        $image->draw()->text(sprintf("下载日期：%s",date("Y-m-d H:i:s")), $font, new Point(30, 160), 0);
        
        if($model->stock == 1){

            $image->draw()->line(new Point(600,200),new Point(600,$y-20),$palette->color('000'));
            $image->draw()->line(new Point(720,200),new Point(720,$y-20),$palette->color('000'));
            $image->draw()->line(new Point(970,200),new Point(970,$y-20),$palette->color('000'));
            $image->draw()->line(new Point(1200,200),new Point(1200,$y-20),$palette->color('000'));

            $image->draw()->text('类型', $font, new Point(40, 220), 0);
            $image->draw()->text('人数', $font, new Point(640, 220), 0);
            $image->draw()->text('价格', $font, new Point(760, 220), 0);
            $image->draw()->text('同业价格', $font, new Point(1010, 220), 0);
            $image->draw()->text('剩余', $font, new Point(1240, 220), 0);

            $image->draw()->line(new Point(20,260),new Point(1380,260),$palette->color('000'));

            $key = 1;
            $height = 60;
            if($all_rooms){
                foreach($all_rooms as $rooms){
                    $price = $price_t = '';
                    $p_num = 0;
                    if(isset($rooms->sell_out) && $rooms->sell_out == 1){
                        $price = '售罄';
                        $price_t = '售罄';
                        $p_num = '售罄';
                    }else{ 
                        $price = (!empty($rooms->price))?sprintf("¥%s",$rooms->price):'--';
                        $price_t = (!empty($rooms->inside_price))?sprintf("¥%s",$rooms->inside_price):'--';
                        $p_num = ($rooms->num-$rooms->stockNum >= 10) ? "9+" : $rooms->num-$rooms->stockNum ;
                    } 
                   
                    $image->draw()->text($rooms->room->name.$rooms->room->code, $font, new Point(30, 220+$key*$height), 0);
                    $image->draw()->text($rooms->people.'人', $font, new Point(640, 220+$key*$height), 0);
                    $image->draw()->text($price, $font, new Point(760, 220+$key*$height), 0);
                    $image->draw()->text($price_t, $font, new Point(1010, 220+$key*$height), 0);
                    $image->draw()->text($p_num, $font, new Point(1240, 220+$key*$height), 0);

                    $image->draw()->line(new Point(20,260+$key*$height),new Point(1380,260+$key*$height),$palette->color('000'));

                    $key++; 
                }
            }
            
        }else if($model->stock == 2){
            //3条竖线
            $image->draw()->line(new Point(600,200),new Point(600,$y-20),$palette->color('000'));
            $image->draw()->line(new Point(900,200),new Point(900,$y-20),$palette->color('000'));
            $image->draw()->line(new Point(1200,200),new Point(1200,$y-20),$palette->color('000'));

            $image->draw()->text('类型', $font, new Point(40, 220), 0);
            $image->draw()->text('第1/2人价格', $font, new Point(640, 220), 0);
            $image->draw()->text('第3/4人价格', $font, new Point(940, 220), 0);
            $image->draw()->text('剩余', $font, new Point(1240, 220), 0);
            $image->draw()->line(new Point(20,260),new Point(1380,260),$palette->color('000'));
            $key = 1;
            $height = 60;
            if($all_rooms){
                foreach($all_rooms as $rooms){
                    $price_1 = $price_2 = '';
                    $num_1 = 0;
                    if(isset($rooms['sell_out']) && $rooms['sell_out'] == 1){
                        $price_1 = $price2 = '售罄';
                        $num_1 = '售罄';  
                    }else{
                        $price_1 = (!empty($rooms['price']))?sprintf("¥%s",$rooms['price']):'--';
                        $price_1 .= ' / ';
                        $price_1 .= (!empty($rooms['inside_price']))?sprintf("¥%s",$rooms['inside_price']):'--';

                        $price_2 = (!empty($rooms['price2']))?sprintf("¥%s",$rooms['price2']):'--';
                        $price_2 .= ' / ';
                        $price_2 .= (!empty($rooms['inside_price2']))?sprintf("¥%s",$rooms['inside_price2']):'--';
                        $num_1 = ($rooms['over_num'] >= 10) ? "9+":$rooms['over_num'] ;
                    } 

                    $image->draw()->text($rooms['name'], $font, new Point(40, 220+$key*$height), 0);
                    $image->draw()->text($price_1, $font, new Point(640, 220+$key*$height), 0);
                    $image->draw()->text($price_2, $font, new Point(940, 220+$key*$height), 0);
                    $image->draw()->text($num_1, $font, new Point(1240, 220+$key*$height), 0);
                    $image->draw()->line(new Point(20,260+$key*$height),new Point(1380,260+$key*$height),$palette->color('000'));
                    $key++; 
                }
            }
        }
       

        $img_path = sprintf("uploads/image/1000%d.jpg",$model->id);

        if($image->save($img_path)){
            print_r(json_encode(['code'=>1,'path'=>\yii::$app->request->getHostInfo().'/'.$img_path]));
        }else{
            print_r(json_encode(['code'=>0]));
        }
    
    }


}
